//
//  DRSettingViewController.m
//  DoctorRen
//
//  Created by Lemon on 2018/11/19.
//

#import "DRSettingViewController.h"

@interface DRSettingViewController ()
@property (weak, nonatomic) IBOutlet UISwitch *notiSwitch;

@end

@implementation DRSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.tableFooterView = [UIView new];
    self.title = @"设置";
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            [self pushWebViewWithUrl:[NSString stringWithFormat:@"http://rsjkkj.com/dist/#/my/accountSet?token=%@&isBack=1",[DRLoginDataModel getUserToken]]];
            self.hidesBottomBarWhenPushed = YES;
            break;
        case 2:
            [self showClearCacheAlert];
            break;
        case 3:
            [self pushWebViewWithUrl:[NSString stringWithFormat:@"http://rsjkkj.com/dist/#/my/opinion?token=%@&isBack=1",[DRLoginDataModel getUserToken]]];
            self.hidesBottomBarWhenPushed = YES;
            break;
        case 4:
            [self showGradedViewController];
            break;
        case 5:
            [self pushWebViewWithUrl:[NSString stringWithFormat:@"http://rsjkkj.com/dist/#/aboutMe?token=%@&isBack=1",[DRLoginDataModel getUserToken]]];
            self.hidesBottomBarWhenPushed = YES;
            break;
        case 6:
            [self showLoginOutAlert];
            break;
        default:
            break;
    }
}

-(void) showGradedViewController{
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DRGradedViewController"];
    //把当前控制器作为背景
    self.definesPresentationContext = YES;
    
    //设置模态视图弹出样式
    vc.modalPresentationStyle = UIModalPresentationOverFullScreen;
    vc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.row == 2) {
        //获取缓存的大小
        NSUInteger intg = [[SDImageCache sharedImageCache] getSize];
        //
        NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:intg]];
        cell.detailTextLabel.text = currentVolum;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (IBAction)setNotification:(id)sender {
    
}

-(void)logout{
    DRUserModel *userModel = [DRUserModel new];
    userModel.phone = [DRLoginDataModel getUserModel].phone;
//    userModel.token = @"";
    [DRLoginDataModel saveUserModel:userModel];
    [DRLoginDataModel setIsLogin:NO];
    UIStoryboard *loginSB = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
    [UIApplication sharedApplication].keyWindow.rootViewController = loginSB.instantiateInitialViewController;
    [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
}

-(void) showLoginOutAlert{
    WEAKSELF
    [self presentAlertViewWithTitle:@"是否退出登录？" message:@"" cancelTitle:@"取消" defaultTitle:@"确定" distinct:YES cancel:^{
        
    } confirm:^{
        [weakSelf logout];
    }];
}

-(void) showClearCacheAlert{
    WEAKSELF
    [self presentAlertViewWithTitle:@"是否清除缓存？" message:@"" cancelTitle:@"取消" defaultTitle:@"确定" distinct:YES cancel:^{
        
    } confirm:^{
        [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
            [weakSelf.tableView reloadData];
        }];
    }];
}

//计算出大小
- (NSString *)fileSizeWithInterge:(NSInteger)size{
    // 1k = 1024, 1m = 1024k
    if (size < 1024) {// 小于1k
        return [NSString stringWithFormat:@"%ldB",(long)size];
    }else if (size < 1024 * 1024){// 小于1m
        CGFloat aFloat = size/1024;
        return [NSString stringWithFormat:@"%.0fK",aFloat];
    }else if (size < 1024 * 1024 * 1024){// 小于1G
        CGFloat aFloat = size/(1024 * 1024);
        return [NSString stringWithFormat:@"%.1fM",aFloat];
    }else{
        CGFloat aFloat = size/(1024*1024*1024);
        return [NSString stringWithFormat:@"%.1fG",aFloat];
    }
}

@end
